package com.ssmk.adapter;

import android.content.Context;
import android.graphics.Color;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RadioButton;
import android.widget.TextView;

import com.blankj.utilcode.util.LogUtils;
import com.bumptech.glide.Glide;
import com.ssmk.R;
import com.ssmk.module.home.utils.FontHelper;
import com.ssmk.module.merchant.bean.ShoppingFeileiBean;
import com.ssmk.module.merchant.view.RecyclerViewListener;
import com.zhy.autolayout.utils.AutoUtils;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;


public class Marter_head_adapter extends RecyclerView.Adapter implements View.OnClickListener {

    private List<ShoppingFeileiBean.MerchantsTypeDataBean> list = new ArrayList<>();
    private Context context;
    private OnItemClickListener mListener = null;
    public int index = -1;

    public Marter_head_adapter(Context context) {
        this.context = context;
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(context).inflate(R.layout.item_layout, null);
        MyViewHolder myViewHolder = new MyViewHolder(view);

       view.setOnClickListener(this);

        return myViewHolder;
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        if (holder instanceof MyViewHolder){
            MyViewHolder myHolder = (MyViewHolder) holder;
            Glide.with(context).load(list.get(0).getCheckIcon()).into(myHolder.ivIcon);
            myHolder.tvName.setTextColor(Color.rgb(254, 23, 23));
            if (index == position){
                Glide.with(context).load(list.get(index).getCheckIcon()).into(myHolder.ivIcon);
                myHolder.tvName.setText(list.get(index).getTypeName());
                myHolder.tvName.setTextColor(Color.rgb(254, 23, 23));

            }else {
                Glide.with(context).load(list.get(position).getOriginalIcon()).into(myHolder.ivIcon);
                myHolder.tvName.setText(list.get(position).getTypeName());
                myHolder.tvName.setTextColor(Color.rgb(42,42,42));
            }
        }
        holder.itemView.setTag(position);
    }

    @Override
    public int getItemCount() {
        return list.size()>0 ? list.size():0;
    }

    @Override
    public void onClick(View v) {
        if (mListener != null) {
            mListener.onItemClick(v, (Integer) v.getTag());
        }
    }

    class MyViewHolder extends RecyclerView.ViewHolder {

        @BindView(R.id.iv_icon)
        ImageView ivIcon;
        @BindView(R.id.tv_name)
        TextView tvName;

        public MyViewHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
            AutoUtils.auto(itemView);
            FontHelper.applyFont(context, tvName, FontHelper.WRYHL);
        }
    }

    public interface OnItemClickListener{
        void onItemClick(View view,int posit);
    }

    public void setOnItemClickListener(OnItemClickListener listener) {
        this.mListener = listener;
    }

    public void setData(List<ShoppingFeileiBean.MerchantsTypeDataBean> datas) {
        if (datas != null) {
            list.addAll(datas);
        }
    }

}
